package com.geely.club.util.common;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by mengyangyang on 2015-10-15.
 */
public class NetUtil {
    private static Context context;

    /**
     * 检测网络是否可用
     *
     * @return
     */
    public boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }

    public enum Type {NO_CONNECTION, WIFI, HONEYCOMB}

    public Type getNetworkType() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return Type.NO_CONNECTION;
        }
        int nType = networkInfo.getType();
        switch (networkInfo.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                return Type.WIFI;
            default:
                return Type.HONEYCOMB;
        }

    }
}
